/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bo;

import org.ipti.br.tag.dao.UnityFacade;
import org.ipti.br.tag.entity.Actor;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Organization;
import org.ipti.br.tag.entity.Unity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;

/**
 *
 * @author root
 */
@Stateless
public class UnityBO implements Serializable {

    @Inject
    private UnityFacade unityDAO;

    public UnityFacade getUnityDAO() {
        return unityDAO;
    }

    public Unity getSchool(Unity unityFather){
        return unityDAO.getSchool(unityFather);
    }
    
    public List<Actor> getStudentsBySchool(Unity unity){
        return unityDAO.getStudentsBySchool(unity);
    }
    
    public void create(Unity unity) {
        getUnityDAO().create(unity);
    }

    public void edit(Unity unity) {
        getUnityDAO().edit(unity);
    }

    public void remove(Unity unity) {
        getUnityDAO().remove(unity);
    }

    public Unity find(Object id) {
        return getUnityDAO().find(id);
    }

    public Unity find(Unity unity) {
        return getUnityDAO().find(unity);
    }

    public List<Unity> findAll() {
        return getUnityDAO().findAll();
    }
    
    public List<Unity> getUnityChildren(Long unityID) {
        return getUnityDAO().getUnityChildren(unityID);
    }

    public List<Unity> getUnitiesPerOrganization(Long organizationID) {
        return getUnityDAO().getUnitiesPerOrganization(organizationID);
    }

    public List<Unity> getUnitiesPerOrganizationUnityFather(Long organizationID, Long unityFatherID) {
        return getUnityDAO().getUnitiesPerOrganizationUnityFather(organizationID, unityFatherID);
    }

    public List<Unity> getUnitiesPerOrganizationUnityFatherYear(Long organizationID, Long unityFatherID, int year) {
        List<Unity> unityList = new ArrayList<Unity>();
        for (Unity u : getUnityDAO().getUnitiesPerOrganizationUnityFather(organizationID, unityFatherID)) {
            if (u.getActivated().get(Calendar.YEAR) == year) {
                unityList.add(u);
            }
        }
        return unityList;
    }

    public List<Unity> unityChildrenPerOrganization(Unity unity, Organization organization) {
        List<Unity> list = new ArrayList<Unity>();

        if (!unity.getOrganization().getId().equals(organization.getId())) {
            for (Unity u : getUnityDAO().getUnityChildren(unity.getId())) {
                list.addAll(unityChildrenPerOrganization(u, organization));
            }
        } else {
            list.add(unity);
        }
        return list;
    }
    
    public List<Long> unityChildrenPerOrganization2(Long unityID, Long organizationID) {
        List<Long> list = new ArrayList<Long>();

        Unity unityAux = getUnityDAO().find(unityID);

        if (unityAux.getOrganization().getId().longValue() != organizationID.longValue()) {
            for (Unity u : getUnityDAO().getUnityChildren(unityAux.getId())) {
                list.addAll(unityChildrenPerOrganization2(u.getId(), organizationID));
            }
        } else {
            list.add(unityAux.getId());
        }
        return list;
    }

    public List<Unity> unityChildrenPerOrganizationYear(Unity unity, Organization organization, int year) {
        List<Unity> list = new ArrayList<Unity>();
        
        System.out.println("unity: "+unity.getName()+ " "+unity.getId());
        System.out.println("org: "+organization.getName()+ " "+organization.getId());
        if (unity.getOrganization().getId().equals(organization.getId())) {
            for (Unity u : getUnityDAO().getUnityChildren(unity.getId())) {
                list.addAll(unityChildrenPerOrganizationYear(u, organization, year));
            }
        } else {
            if (unity.getActivated().get(Calendar.YEAR) == year) {
                list.add(unity);
            }
        }
        return list;
    }

    public List<Unity> unityChildren(Unity unity, Organization organization, int year) {
        List<Unity> list = new ArrayList<Unity>();
        
        for (Unity u : getUnityDAO().getUnityChildren(unity.getId())) {
            if (isActive(u, year) && u.getOrganization().getId().equals(organization.getId())) {
                list.add(u);
            } else {
                list.addAll(unityChildren(u, organization, year));
            }
        }
        return list;
    }
    
    public List<Long> unityChildrenPerOrganizationYearID(Long unityID, Long organizationID, int year) {
        List<Long> list = new ArrayList<Long>();

        Unity unityAux = getUnityDAO().find(unityID);

        if (unityAux.getOrganization().getId().longValue() != organizationID.longValue()) {
            for (Unity u : getUnityDAO().getUnityChildren(unityAux.getId())) {
                list.addAll(unityChildrenPerOrganizationYearID(u.getId(), organizationID, year));
            }
        } else {
            if (unityAux.getActivated().get(Calendar.YEAR) == year) {
                list.add(unityAux.getId());
            }
        }
        return list;
    }    
    public List<Long> unityActivatedChildren(Long unityID, Long organizationID, int year) {
        List<Long> list = new ArrayList<Long>();

        Unity unityAux = getUnityDAO().find(unityID);

        if (unityAux.getOrganization().getId().longValue() != organizationID.longValue()) {
            for (Unity u : getUnityDAO().getUnityChildren(unityAux.getId())) {
                Calendar now = Calendar.getInstance();
                if(u.getDeactivated() == null || now.compareTo(u.getDeactivated()) < 0) {
                    list.addAll(unityActivatedChildren(u.getId(), organizationID, year));
                }
            }
        } else {
            if (unityAux.getActivated().get(Calendar.YEAR) == year) {
                list.add(unityAux.getId());
            }
        }
        return list;
    }

    public List<Unity> searchUnityPerNameOrganization(String unityName, Long organizationID) {
        return getUnityDAO().searchUnityPerNameOrganization(unityName, organizationID);
    }

    public List<Unity> modifiedUnitiesBetweenDate(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        return getUnityDAO().modifiedUnitiesBetweenDate(initialDate, finalDate, serialNumber);
    }

    public List<Unity> modifiedUnitiesBetweenDateWithFather(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        return getUnityDAO().modifiedUnitiesBetweenDateWithFather(initialDate, finalDate, serialNumber);
    }

    public List<Unity> activatedUnities(List<Long> unities) {
        return getUnityDAO().activatedUnities(unities);
    }

     public boolean isActive(Unity unity, int year) {
        if((unity.getActivated() != null && unity.getActivated().get(Calendar.YEAR) > year) ||
                (unity.getDeactivated() != null && unity.getDeactivated().get(Calendar.YEAR) < year)){
            return false;
        } else {
            return true;
        }
    }
    
    public List<Unity> unitiesPerID(List<Long> unities) {
        return getUnityDAO().unitiesPerID(unities);
    }
    
    public List<Unity> findAllOrder() {
        return getUnityDAO().findAllOrder();
    }
    
    public List<Unity> getClassLessonList(Unity unitySchool, int year) {
        List<Unity> sectorEducational = findSectorEducational(getUnityChildren(unitySchool.getId()),year);
        sectorEducational.add(unitySchool);
        return sectorEducational;
    }
    
    public List<Unity> findSectorEducational(List<Unity> unityList, int year) {   
        List<Unity> temp = new ArrayList<>();
        
        for (Unity element : unityList) {
            if (element.getOrganization().getName().equals("Fundamental Menor")
                || element.getOrganization().getName().equals("Fundamental Maior")
                || element.getOrganization().getName().equals("EJA")
                || element.getOrganization().getName().equals("Educação Infantil")) {
                for(Unity unityChildren : getUnityChildren( element.getId())) {
                    if (isActive(unityChildren, year)) {
                        temp.add( unityChildren );
                    }
                }
            } else {
                temp.addAll(findSectorEducational(getUnityChildren(element.getId()), year));
            }
        }
        
        return temp;
    }
}